package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import com.sun.xml.bind.v2.runtime.reflect.Lister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//套餐列表
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(
            @RequestParam(name = "page") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            String name) { // 接受前端GET传来的参数
        // 2.调用service
        Page<Setmeal> page = setmealService.findByPage(pageNum,pageSize,name);
        // 3.返回成功
        return ResultInfo.success(page);
    }

    // 新增套餐
    @PostMapping("/setmeal")
    // 1.接收参数
    public ResultInfo save(@RequestBody Setmeal setmeal) {
        // 2.调用service
        setmealService.save(setmeal);
        // 3.返回结果
        return ResultInfo.success();
    }

    //回显套餐
    @GetMapping("/setmeal/{id}")
    // 1.接收前端传来的参数
    public ResultInfo findById(@PathVariable Long id) {
        // 2.调用service
        Setmeal setmeal = setmealService.findById(id);
        // 3.返回结果
        return ResultInfo.success(setmeal);
    }

    // 修改套餐
    @PutMapping("/setmeal")
    // 1.接收参数
    public ResultInfo updateById(@RequestBody Setmeal setmeal) {
        // 2.调用service
        setmealService.updateById(setmeal);
        //3.返回结果
        return ResultInfo.success();
    }

    // 删除套餐
    @DeleteMapping("/setmeal")
    // 1.接收参数
    public ResultInfo deleteBatch(@RequestParam List<Long> ids) {
        // 2.调用service
        setmealService.deleteBatch(ids);
        // 3.返回结果
        return ResultInfo.success();
    }

    //停售套餐
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //调用service
        setmealService.updateStatus(status,ids);
        //返回结果
        return ResultInfo.success();
    }
}
